package com.zhuhanpeng.cloud.spring.security;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

@Component

public class GdmtPasswordEncoder implements PasswordEncoder {
    @Value("${passwd.key}")
    private String passwdKey;

    @Override
    public String encode(CharSequence charSequence) {
        if (charSequence == null) {
            throw new IllegalArgumentException("charSequence cannot be null");
        }
            String rst = DigestUtils.md5DigestAsHex((charSequence + passwdKey).getBytes());
              return  rst;
        }
    @Override

    public boolean matches(CharSequence charSequence, String s) {
        if (charSequence == null) {

            throw new IllegalArgumentException("charSequence cannot be null");
        }
        if (s == null) {
            throw new IllegalArgumentException("db password cannot be null");
        }
        return s.equals(encode(charSequence));
    }
}
